﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;


namespace FTClient
{
  /// <summary>
  /// Description of MainForm.
  /// </summary>
  
  public partial class MainForm : Form
  {
    
    public this()
    {
      InitializeComponent();
    }
    

    private mutable Server:ThreadedServer;
//    private mutable fileList:List[FileInfo]=List();

    
        private btnConnect_Click (sender : object,  e : System.EventArgs) : void
        {
//        mutable fileList:List[oneFileInfo]=List();
            if (Server==null)
            {
                _ = MessageBox.Show("Клиентский сокет не запущен");
            }
            else
            {
                try
                {
                    GlobalServer.Refresh(tbxServerName.Text, Convert.ToInt32(tbxServerPort.Text));
                    GlobalServer.Connect(Server.fileList,Server._serverSocket);
                    _ = Connected();              
                }
                catch
                {
                    | e => _ = MessageBox.Show($"Connection Exception: $(e.Message)");

                }
                //btnConnect.Text="test";
            }
        }

        private MainForm_Load (sender : object,  e : System.EventArgs) : void
        {
            ;            
        }

        private btnDownloadFile_Click (sender : object,  e : System.EventArgs) : void
        {
            when (lbxServerFiles.SelectedItem !=null)
            {   
//                def clientName="ctg25"; //GetClient name     
//                def clientPort=2222;//Get client port
                def hash=GlobalServer.FindHash(lbxServerFiles.SelectedItem.ToString());
//                def hash=5;
                saveDownloadedFile.FileName=lbxServerFiles.SelectedItem.ToString();
                _ = saveDownloadedFile.ShowDialog();
                try
                {
                    _ = GetSocket.SocketSendReceive(saveDownloadedFile.FileName,hash);//lbxServerFiles.SelectedItem.ToString());
                }
                catch
                {
                    | e => _ = MessageBox.Show($"Connection Exception: $(e.Message)");

                }
                    
            }
        }

        private btnCStart_Click (sender : object,  e : System.EventArgs) : void
        {
            def _port=Convert.ToInt32(tbxClientsPort.Text);
            Server= ThreadedServer(System.Net.IPAddress.Parse(tbxClientName.Text),_port); 
            _ = Server.Start();
            btnCStop.Enabled=true;
            tbxClientsPort.Enabled=false;
            btnCStart.Enabled=false;
            btnAddFile.Enabled=false;
            btnRemoveFile.Enabled=false;
            btnConnect.Enabled=true;
//            btnDisconnect.Enabled=false;
            foreach (oneFile:string in lbxFilesList.Items)
            {
                mutable FileName=oneFile.Substring(oneFile.LastIndexOf("\\")+1);
                when (FileName.Equals(oneFile))
                    FileName=oneFile.Substring(oneFile.LastIndexOf("/")+1);
                Server.AddFile(FileName,oneFile);
            }
        }

        private btnCStop_Click (sender : object,  e : System.EventArgs) : void
        {
            btnCStop.Enabled=false;
            tbxClientsPort.Enabled=true;
            btnCStart.Enabled=true;
            btnAddFile.Enabled=true;
            btnRemoveFile.Enabled=true;
            btnDisconnect.Enabled=false;
            Disconnected();
            btnConnect.Enabled=false;
//            GlobalServer.Disconnect();

            _ = Server.Stop();
        }

        private btnAddFile_Click (sender : object,  e : System.EventArgs) : void
        {
            openAddFile.ShowDialog();
            lbxFilesList.Items.Add(openAddFile.FileName.ToString());
            
        }

        private btnRemoveFile_Click (sender : object,  e : System.EventArgs) : void
        {
            lbxFilesList.Items.Remove(lbxFilesList.SelectedItem);
        }

        private btnDisconnect_Click (sender : object,  e : System.EventArgs) : void
        {
            try
            {
                Disconnected();
                GlobalServer.Disconnect();
            }
            catch
            {
                | e => _ = MessageBox.Show($"Connection Exception: $(e.Message)");

            }
          
        }

        private btnSearch_Click (sender : object,  e : System.EventArgs) : void
        {
          try
          {
            
            _ = GlobalServer.Search(tbxSearch.Text);
            lbxServerFiles.Items.Clear();
            foreach (oneFile:oneFileInfo in GlobalServer.fileList)
            {
                lbxServerFiles.Items.Add(oneFile.filename);
            }
          }
            catch
            {
                | e => _ = MessageBox.Show($"Connection Exception: $(e.Message)");

            }

        }

        private lbxServerFiles_SelectedIndexChanged (sender : object,  e : System.EventArgs) : void
        {
            ;
        }
        private Connected():void
        {
                btnDisconnect.Enabled=true;
                btnConnect.Enabled=false;
                tbxServerName.Enabled=false;
                tbxServerPort.Enabled=false;
                btnSearch.Enabled=true;
                lbxServerFiles.Enabled=true;
                tbxSearch.Enabled=true;
                btnDownloadFile.Enabled=true;                
        }
        private Disconnected():void
        {
                btnDisconnect.Enabled=false;
                btnConnect.Enabled=true;
                tbxServerName.Enabled=true;
                tbxServerPort.Enabled=true;
                btnSearch.Enabled=false;
                lbxServerFiles.Enabled=false;
                tbxSearch.Enabled=false;
                btnDownloadFile.Enabled=false; 
        }

        private groupBox4_Enter (sender : object,  e : System.EventArgs) : void
        {
            ;
        }
  }
}
